package com.dao;

import com.bean.User;
import com.utils.JdbcUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hecn
 * @version 1.0
 * @date 2020/8/1 0001 15:45
 * @company
 */
public class UserDao {
    /***
     * 用户登录
     * @param name
     * @param password
     * @return
     */
    public User login(String name, String password) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JdbcUtils.getConnection();
            String sql = "select * from reader where name=? and password=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    user = new User();
                    user.setName(resultSet.getString(1));
                    user.setPassword(resultSet.getString(2));
                    System.out.println("登录成功");
                } else {
                    System.out.println("登录失败");
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(connection, preparedStatement, resultSet);
        }
        return user;
    }

    /***
     * 用户注册（添加用户）
     * @param user
     */
    public void addUser(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JdbcUtils.getConnection();
            String sql = "insert into reader(name, password, id, sex, telephone, email) VALUES(?,?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, user.getName());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getId());
            preparedStatement.setString(4, user.getSex());
            preparedStatement.setString(5, user.getTelephone());
            preparedStatement.setString(6, user.getEmail());

            preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(connection, preparedStatement);
        }
    }

    /***
     * 判断用户是否已经纯在
     * @param username
     * @return
     */
    public boolean isExist(String username) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from book.reader where name=?";
        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,username);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    /***
     * 列出书库中所有图书
     */
    public List<String> showBook() {
        List<String> list = new ArrayList<>();
        int i = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from book.book";

        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                String message = "书名：" + resultSet.getString("book_name")
                        + "<--->剩余数量：" + resultSet.getInt("book_num")
                        + "<--->作者为：" + resultSet.getString("book_writer")
                        + "<--->书的价格为：" + resultSet.getInt("book_price")
                        + "<--->出版时间为：" + resultSet.getString("publish_time")
                        + "<--->出版社为：" + resultSet.getString("publish_house");
                list.add(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(connection, preparedStatement, resultSet);
        }

        return list;
    }


}
